package club.justwill.single.assembler;

import club.justwill.single.bean.ProjectIntroduceResp;
import club.justwill.single.bean.ProjectsResp;
import club.justwill.single.bean.SkusResp;
import club.justwill.single.persistence.document.ProjectSupport;
import club.justwill.single.persistence.jpo.ProjectIntroduce;
import club.justwill.single.persistence.jpo.Projects;
import club.justwill.single.persistence.jpo.Skus;
import club.justwill.single.persistence.jpo.User;
import club.justwill.single.persistence.store.ProjectSupportStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiangnan on 15-12-26.
 */
@Component
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class ProjectAssemble {
    @Autowired
    private ProjectSupportStore projectSupportStore;

    public List<ProjectsResp> assembleProjects(List<Projects> projectsList){
        List<ProjectsResp> projectsResps = new ArrayList<ProjectsResp>();
        for(Projects projects : projectsList) {
            projectsResps.add(assembleProject(projects));
        }
        return projectsResps;
    }

    public ProjectsResp assembleProject(Projects project) {
        ProjectsResp projectsResp = assembleProjectMini(project);
        for(ProjectIntroduce pji : project.getProjectIntroduceSet()) {
            projectsResp.getIntroduceList().add(new ProjectIntroduceResp(pji));
        }
        for(Skus sku: project.getSkusSet()) {
            projectsResp.getSkusList().add(new SkusResp(sku));
        }
        /*for(Orders order:project.getOrdersSet()) {
            projectsResp.getOrdersRespList().add(new OrdersResp(order));
        }*/
        return projectsResp;
    }

    public List<ProjectsResp> assembleProjectMiniLisit(List<Projects> projectList) {
        List<ProjectsResp> projectsRespList = new ArrayList<ProjectsResp>();
        for(Projects project : projectList) {
            projectsRespList.add(assembleProjectMini(project));
        }
        return projectsRespList;
    }
    public ProjectsResp assembleProjectMini(Projects project) {
        ProjectsResp projectsResp = new ProjectsResp(project);
        projectsResp.setProjectId(project.getProjectId()+"");
        projectsResp.setUserId(project.getUserId()+"");
        projectsResp.setTitle(project.getTitle()+"");
        projectsResp.setAdvantage(project.getAdvantage()+"");
        projectsResp.setCover(project.getCover()+"");
        projectsResp.setName(project.getName()+"");
        projectsResp.setType(project.getType()+"");
        projectsResp.setFinancing(project.getFinancing()+"");
        projectsResp.setOverflow(project.getOverflow()+"");
        projectsResp.setEndTime(project.getEndTime()==null?"":project.getEndTime().getTime()+"");
        projectsResp.setTags(project.getTags()+"");
        projectsResp.setStartMoney(project.getStartMoney()+"");
        projectsResp.setEveryMaxBuy(project.getEveryMaxBuy()+"");
        projectsResp.setPublishTime(project.getPublishTime()==null?"":project.getPublishTime().getTime()+"");
        projectsResp.setStatus(project.getStatus()+"");
        projectsResp.setProvince(project.getProvince()+"");
        projectsResp.setCity(project.getCity()+"");
        projectsResp.setArea(project.getArea()+"");
        projectsResp.setModelId(project.getModelId()+"");
        projectsResp.setDispatchTime(project.getDispatchTime()==null?"":project.getDispatchTime().getTime()+"");
        ProjectSupport projectSupport =  projectSupportStore.searchProjectSupport(project.getProjectId());
        if(projectSupport != null) {
            projectsResp.setGetFinancing(projectSupport.getGetFinancing()==null? "0" : projectSupport.getGetFinancing()+"");
            projectsResp.setPeopleNum(projectSupport.getPeopleNum()==null? "0" : projectSupport.getPeopleNum()+"");
        } else {
            projectsResp.setGetFinancing("0");
            projectsResp.setPeopleNum("0");
        }
        return projectsResp;
    }

}
